<?php defined('IN_HOPECMS') or exit('Access Denied'); ?>
<div class="content-title">栏目管理</div>
<div class="clear margin-top-10"></div>
<link rel="stylesheet" type="text/css" href="{__ROOT__}app/widget/listview/view/listview.css">
<div id="listview" style="margin-bottom: 100px;">
	<table class="gird-list" cellspacing="0" cellpadding="0" border="0" width="98.5%;">
		<tr class="caption">
			<td width="50" style="cursor:auto;">ID</td>
			<td width="280" style="cursor:auto;">栏目名称</td>
			<td width="50" style="cursor:auto;text-align:center;text-indent: 0px;">排序</td>
			<td width="80" style="cursor:auto;text-align:center">栏目类型</td>
			<td width="80" style="cursor:auto;text-align:center">所属模型</td>
			<td width="60" style="cursor:auto;text-align:center">是否显示</td>
			<td width="150" style="cursor:auto;text-align:center">操作</td>
		</tr>
		<?php
			if (!empty($tree)) :
				foreach ($tree as $t) :
					switch ($t['type']) {
						case 0 :
							$type = '<span class="col-green">列表</span>';
							$addurl  = D::createUrl(
								'category/index/categoryadd', 
								array('type'=>0, 'pid'=>$t['cat_id'], 'mid'=>$t['model_id'])
							);
							$editurl = D::createUrl(
								'category/index/categoryedit',
								array('type'=>0, 'cid'=>$t['cat_id'], 'mid'=>$t['model_id'])
							);
							break;
						case 1 :
							$type = '<span class="col-blue">单页</span>';
							$addurl  = D::createUrl(
								'category/index/categoryadd', 
								array('type'=>1, 'pid'=>$t['cat_id'])
							);
							$editurl = D::createUrl(
								'category/index/categoryedit',
								array('type'=>1, 'cid'=>$t['cat_id'])
							);
							break;
						case 2 :
							$type = '<span class="col-red">跳转</span>';
							$addurl  = D::createUrl(
								'category/index/categoryadd', 
								array('type'=>2, 'pid'=>$t['cat_id'])
							);
							$editurl = D::createUrl(
								'category/index/categoryedit',
								array('type'=>2, 'cid'=>$t['cat_id'])
							);
							break;
					};

					if ($t['is_show']) {
						$show = '<span style="font-size:14px;" val="'.$t['is_show'].'" class="col-green">√</span>';
					} else {
						$show = '<span style="font-size:14px;" val="'.$t['is_show'].'" class="col-red">×</span>';
					}
					$delurl  = D::createUrl(
						'category/index/categorydelete',
						array('cid'=>$t['cat_id'])
					);
		?>
		<tr class="list">
			<td><?php echo $t['cat_id'];?></td>
			<td><?php echo $t['sname'];?></td>
			<td align="center" style="text-indent: 0px;" class="cg_sort hand" val="<?php echo $t['sort'];?>">
				<div url="?m=category&c=index&a=editsort&cat_id=<?php echo $t['cat_id'];?>">
					<?php echo $t['sort'];?>
				</div>
			</td>
			<td align="center"><?php echo $type;?></td>
			<td align="center"><?php if (isset($model[$t['model_id']])) : echo $model[$t['model_id']]; endif; ?></td>
			<td align="center" class="hand class_show" cid="<?php echo $t['cat_id'];?>"><?php echo $show;?></td>
			<td align="center">
				<a class="col-000" href="javascript:;" onclick="goto('<?php echo $addurl;?>', 1);">添加子栏目</a>
				|
				<a class="col-000" href="javascript:;" onclick="goto('<?php echo $editurl;?>', 2);">修改</a>
				|
				<a class="col-000" href="javascript:;" onclick="del('<?php echo $delurl;?>', this)">删除</a>
			</td>
		</tr>
		<?php
				endforeach;
			endif;
		?>
	</table>
	<style type="text/css">
	.cg_sort div:hover {
		border : 1px dashed #0E70AD;
		background: #fff;
		width  : 25px;
		line-height: 25px;
	}
	</style>
	<script type="text/javascript">
	function goto(url, t)
	{
		var u;
		$('#main_content').load(url);
		if (t == 1) {
			u = '<?php echo D::createUrl('category/index/categoryadd');?>';
		} else {
			u = '<?php echo D::createUrl('category/index/categoryedit');?>';
		}
		top_menu(u);
	}

	function del(url, t)
	{
		dpAlert(this, {
			callback : 'alerts',
			win    : true,
			title  : '提示',
			height : '230',
			content: '确定要删除吗，栏目下的数据也将被删除，该操作不可恢复！'
		});
		$('#ok_button').click(function(){
			ajaxcall(url, function(data){
				if (data.code == 1) {
					dpAlert(this, {callback : 'notice', content : data.msg});
					$(t).parent().parent().fadeOut(50);
				}
			})
		})
	}
	$(function(){
		//排序
		$('.cg_sort').click(function(){
			_this   = $(this);
			var val = _this.attr('val');
			var url = _this.children('div').attr('url');
			_this.html('<input type="text" id="editsort" style="width:15px;text-indent:0;text-align:center;" value="'+val+'">');
			$('#editsort').select();
			$('#editsort').blur(function(){
				v = $(this).val();
				if (/\d+/.test(v)) {
					ajaxcall(url+'&val='+v, function(data) {
						if (data.code != 0) {
							_this.html('<div url="'+url+'">'+v+'</div>').attr('val', v);
						} else {
							_this.html('<div url="'+url+'">'+val+'</div>').attr('val', val);
						}
					});
				} else {
					_this.html('<div url="'+url+'">'+val+'</div>').attr('val', val);
				}
			})
		});
		//是否显示
		$('.class_show').click(function(){
			var value = $(this).text();
			var catid = $(this).attr('cid');
			var url   = '<?php echo D::createUrl('category/index/changeshow'); ?>';
			var show  = $(this).children('span');
			url += '&cat_id='+catid;
			if (value == '√') {
				url += '&show=0';
			} else {
				url += '&show=1';
			}
			ajaxcall(url, function(data){
				if (data.code == 1) {
					if (value == '√') {
						show.addClass('col-red').removeClass('col-green').
						attr('val', 0).text('×');
					} else {
						show.addClass('col-green').removeClass('col-red').
						attr('val', 1).text('√');
					}
				}
			});
		});
	});
	</script>
</div>